<?php

class Upload extends CI_Controller {
	
	function __construct()
	{
		parent::__construct();
		$this->load->database();
		$this->load->dbutil();
		$this->load->model('user/file_model');
		$this->load->helper(array('form', 'url'));
		$this->load->helper('download');
		$this->load->helper('date');
		$this->load->library('Datatables');
		$this->load->library('table');
		$this->load->library('upload');
		$this->load->library('zip');
		$this->load->helper('file');
	}

	function index()
	{	$this->load->database();
		$this->load->model('user/file_model');	
		$this->load->library('form_validation');
		$this->load->helper('file');
		$this->load->helper(array('form', 'url'));
		$meta['page_title'] = "Upload Content";
		$this->load->view('common/header',$meta);
		$this->load->view('user/upload_form', array('error' => ' ' ));
		$this->load->view('common/footer',$meta);
	}
	public function do_upload()
	{	
		$config['upload_path'] = './uploads/';
		
		$this->load->model("user/file_model");
		$this->load->library('Datatables');
		$textarea = $this->input->post('textarea');
		$userfile = $_FILES['userfile']['name'];
		//print_r($userfile); exit();
		$error = "please type message";
		
		$data = array(
		'textarea' => $this->input->post('textarea'),
		'userfile' => $_FILES['userfile']['name'],		
		);
			if (empty($textarea))
			   {
				  $msg['error'] = "Please enter a message!";
				  $this->load->view('user/upload_form',$msg);
			   }   
			else{
				$userfile = $_FILES['userfile']['name'];
				$tmpName  = $_FILES['userfile']['tmp_name'];
				$timedata=array('textarea'=>$textarea,'time' => date('Y-m-d H:i:s',now()));	
				$id = $this->file_model->insert2($timedata);
				//$this->file_model->insert_file($userfile);
				for($i=0;$i<count($userfile);$i++){
					$this->file_model->insert($userfile[$i],$id);
				}
				for($i=0;$i<count($userfile);$i++){
					$data =array( 'upload_data' => move_uploaded_file($tmpName[$i],"./uploads/main/$userfile[$i]"));
					//print_r($data);exit();
					$this->file_model->thumbnails($userfile[$i],$id);
				}
			
			
				$data['query'] = $this->file_model->show($id);
			
				$meta['page_title'] = "Upload Content";
				$this->load->view('common/header',$meta);
				$this->load->view('user/upload_success',$data);
				$this->load->view('common/footer',$meta);
				
			
		}	
	}
	public function do_adupload(){
		$config['upload_path'] = './uploads/';
		$this->load->model("file_model");
		$this->load->library('Datatables');
		$userfile = $_FILES['userfile']['name'];
		//print_r($userfile); exit();
		$tmpName  = $_FILES['userfile']['tmp_name'];
		for($i=0;$i<count($userfile);$i++){
					$this->file_model->reinsert($userfile[$i],$previd);
				}
				for($i=0;$i<count($userfile);$i++){
					$data =array( 'upload_data' => move_uploaded_file($tmpName[$i],"./uploads/main/$userfile[$i]"));
					//print_r($data);exit();
				}
				for($i=0;$i<count($userfile);$i++){
					$this->file_model->rethumbnails($userfile[$i],$previd);
				}
						
			$data['query'] = $this->file_model->show($previd);
			
				$meta['page_title'] = "Upload Content";
				$this->load->view('common/header',$meta);
				$this->load->view('user/upload_success',$data);
				$this->load->view('common/footer',$meta);
			
	}
	public function get_all(){
		$meta['page_title']  = "Uploaded List";
		$data['query'] = $this->file_model->getall();
		$meta['page_title'] = "Upload Content";
		$this->load->view('common/header',$meta);
		$this->load->view('user/view_all',$data);
		$this->load->view('common/footer',$meta);
	}
	public function do_reupload($previd){
		//echo $id; exit();
		$config['upload_path'] = './uploads/';
		$config['allowed_types'] = 'gif|jpg|png|jpeg|doc|txt|pdf|zip|rar';
		$config['max_size']	= '20480';
		$config['max_width']  = '1024';
		$config['max_height']  = '768';
		$this->load->library('upload',$config);
		$userfile = $_FILES['userfile']['name'];
		$tmpName  = $_FILES['userfile']['tmp_name'];
		for($i=0;$i<count($userfile);$i++){
					$this->file_model->reinsert($userfile[$i],$previd);
				}
				for($i=0;$i<count($userfile);$i++){
					$data =array( 'upload_data' => move_uploaded_file($tmpName[$i],"./uploads/main/$userfile[$i]"));
					//print_r($data);exit();
				}
				for($i=0;$i<count($userfile);$i++){
					$this->file_model->rethumbnails($userfile[$i],$previd);
				}
			//$querry=$this->db->query("SELECT * FROM attachment_table WHERE id='$previd'");
				
			$data['query'] = $this->file_model->show($previd);
			$this->load->view('user/upload_success',$data);
	}	
	public function do_download($id){
	
		$query = $this->db->query("SELECT * FROM attachment_table WHERE id='$id'");
		foreach($query->result() as $row){
			$name = $row->userfile; 
		}
		$data= file_get_contents("./uploads/main/$name");	
		force_download($name,$data);
		
	}
	public function remove_file($id){
		//echo $id; exit();
		$query = $this->db->query("SELECT * FROM attachment_table WHERE id='$id'");
		foreach($query->result() as $row){
			$name = $row->userfile; 
			$text_id= $row->text_id;
		}
		$this->file_model->delete_file($id);
		//print_r($data); exit();
		$data['query'] = $this->file_model->show($text_id);
		$this->load->view('user/upload_success',$data);
				
	}
	public function removewhole_file($id){
		$query = $this->db->query("SELECT * FROM attachment_table WHERE id='$id'");
		foreach($query->result() as $row){
			$name = $row->userfile; 
			$text_id= $row->text_id;
		}
		$query=$this->file_model->delete_file($id);
		echo $query;
		$data['query'] = $this->file_model->getall();
		$this->load->view('user/view_all',$data);
		//print_r($query); exit();
	}
	public function checkremoveAll_file($id){
		//echo $key; exit();
		if(isset($_POST['multi_delete'])){
			$query = $this->db->query("SELECT * FROM attachment_table WHERE id='$id'");
		foreach($query->result() as $row){
			$name = $row->userfile; 
			$text_id= $row->text_id;
		}
		$checked_files= $this->input->post('filecheck');
		//print_r($checked_files); exit();
		$this->file_model->delete_checked($checked_files);
		$data['query'] = $this->file_model->show($text_id);
		$meta['page_title'] = "Upload Content";
				$this->load->view('common/header',$meta);
				$this->load->view('user/view_all',$data);
				$this->load->view('common/footer',$meta);
		
		}
		else{
			$query = $this->db->query("SELECT * FROM attachment_table WHERE id='$id'");
			foreach($query->result() as $row){
				$name = $row->userfile; 
				$text_id= $row->text_id;
			}
			$checked_files= $this->input->post('filecheck');
			//print_r($checked_files); exit();
			$file_name = $this->file_model->download_checked($checked_files);
			//print_r($file_name); exit();
			$path= "./uploads/main/";
			foreach($file_name as $row){
				$data2=file_get_contents("./uploads/main/$row");
			}
			//echo $data2; exit();
			foreach($file_name as $row){
				$this->zip->add_data($row,$data2);
			}	
			$this->zip->archive('./uploads/main/myfiles.zip'); 	
			$this->zip->download('myfiles.zip');
			$data['query'] = $this->file_model->show($text_id);
			
				$meta['page_title'] = "Upload Content";
				$this->load->view('common/header',$meta);
				$this->load->view('user/view_all',$data);
				$this->load->view('common/footer',$meta);
			
		}
		
	}
}
?>